$(document).ready(function (){
	view_init();
	event_init();
});

function view_init()
{
	loadingStart();
	
	$("[data-type=dateAll]").each(function (){
		$(this).datetimepicker({
			  timeFormat: "HH:mm:ss",
	          dateFormat: "yy-mm-dd",
			  monthNames: [ "一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月" ]
		});
	});
	
	// 初始化新闻分类
	um_ajax_post(
			"/article/catalog/select.do",
			null,
			function (data){
				if (data && data.catalogs)
				{
					for (var i=0;i<data.catalogs.length;i++)
					{
						$("[data-id=catalog]")
							.append('<option value="'+data.catalogs[i].catalogID+'">'
										+data.catalogs[i].catalogName+'</option>');
					}
				}
			},
			null);
	
	
	list_list(true);
	
	$("#main-content").show();
}

function event_init()
{
	// 查询按钮
	$("#query").click(function (){
		var createTimeStringStart = $("[data-id=createTimeStringStart]").val();
		var createTimeStringEnd = $("[data-id=createTimeStringEnd]").val();
		if (createTimeStringStart && createTimeStringEnd)
		{
			if (moment(createTimeStringStart).isAfter(createTimeStringEnd))
			{
				$("body").umDialog("alert" ,{"content":"开始时间不能大于结束时间"});
				return false;
			}
		}

		var updateTimeStringStart = $("[data-id=updateTimeStringStart]").val();
		var updateTimeStringEnd = $("[data-id=updateTimeStringEnd]").val();
		if (updateTimeStringStart && updateTimeStringEnd)
		{
			if (moment(updateTimeStringStart).isAfter(updateTimeStringEnd))
			{
				$("body").umDialog("alert" ,{"content":"开始时间不能大于结束时间"});
				return false;
			}
		}
		
		var expireTimeStringStart = $("[data-id=expireTimeStringStart]").val();
		var expireTimeStringEnd = $("[data-id=expireTimeStringEnd]").val();
		if (expireTimeStringStart && expireTimeStringEnd)
		{
			if (moment(expireTimeStringStart).isAfter(expireTimeStringEnd))
			{
				$("body").umDialog("alert" ,{"content":"开始时间不能大于结束时间"});
				return false;
			}
		}
		$("#list_tbody").html("");
		list_list(true);
	});
	
	// 新增按钮
	$("#add").click(function (){
		window.location.href = "/article/direction/edit.do";
	});
	
	// 编辑按钮
	$("#list_tbody").delegate("[data-id=edit]" ,"click" ,function (){
		window.location.href = "/article/direction/edit.do?id="+$(this).parent().attr("data-id");
	});
	
	// 评论按钮
	$("#list_tbody").delegate("[data-id=comment]" ,"click" ,function (){
		window.location.href = "/article/direction/comment.do?id="+$(this).parent().attr("data-id");
	});
	
	
	// 预览按钮
	$("#list_tbody").delegate("[data-id=preview]" ,"click" ,function (){
		window.open($(this).parent().attr("data-url")); 
	});
	
	// 删除按钮
	// 商品删除按钮
	$("#list_tbody").delegate("[data-id=delete]" ,"click" ,function (){
		$("body").umDialog("confirm",{
			content:"确认删除此条记录？",
			enter_method:function (self){
				var obj = new Object();
				obj.articleId = $(self).parent().attr("data-id");
				um_ajax_post(
						INDEX_WEBAPP + "/article/delete.do",
						obj,
						function (data){
							$("body").umDialog("close");
							index_show("删除成功");
							list_list();
						},
						null);
			},
			enter_method_param:this
		});
	});
	
	// 翻页事件
	index_initPageEvent("list_list()");
}

function list_list(isFirst)
{
	if (isFirst)
	{
		index_pageObj.init();
	}
	
	loadingStart();
	
	var obj = $("#query_panel").umDataBind("serialize" ,"");
	obj.currentPage = index_pageObj.currentPage;
	obj.pageSize = index_pageObj.pageSize;
	
	um_ajax_post(
			INDEX_WEBAPP + "/article/select.do",
			obj,
			function (data){
				$("#page_record_count").text(data.size);
				_render(data.articles);
			},
			null);
	function _render(data)
	{
		var buffer = [];
		if (data && data.length >0)
		{
			for (var i=0;i<data.length;i++)
			{
				buffer.push('<tr>');
				buffer.push('<td>'+data[i].articleId+'</td>');
				buffer.push('<td>'+data[i].catalogName+'</td>');
				buffer.push('<td title="'+data[i].title+'">'+(data[i].title.length > 6 ? data[i].title.substr(0,6) + "..." : data[i].title)+'</td>');
				buffer.push('<td>'+beautyNull(data[i].createTime)+'</td>');
				buffer.push('<td>'+beautyNull(data[i].updateTime)+'</td>');
			//	buffer.push('<td>'+beautyNull(data[i].expireTime)+'</td>');
				buffer.push('<td>'+(data[i].isHomePage == 1?"是":"否")+'</td>');
				buffer.push('<td>'+(data[i].isHeadline == 1?"是":"否")+'</td>');
				buffer.push('<td>'+(data[i].isTop == 1?"是":"否")+'</td>');
			//	buffer.push('<td>'+(data[i].status == 1?"显示":"隐藏")+'</td>');
			//	buffer.push('<td>'+(data[i].expire == 1?"是":"否")+'</td>');
			//	buffer.push('<td>'+(data[i].isComment == 0?"是":"否")+'</td>');
				buffer.push('<td>'+beautyNull(data[i].adminName)+'</td>');
				buffer.push('<td style="text-align: center" data-id="'+data[i].articleId+'" data-url="'+data[i].httpPath+'"><a href="#" data-id=edit>编辑 |</a><a href="#" data-id=preview> 预览 |</a><a href="#" data-id=comment> 评论|</a><a href="#" data-id=delete> 删除</a></td>');
				buffer.push('<tr>');
			}
			$("#list_tbody").html(buffer.join(""));
		}
		else
		{
			index_pageObj.toEndPage();
		}

		index_pageObj.isEndMethod(data);

	}
}

function get_check_str(checkCode)
{
	if (checkCode == 0)
	{
		return "未审核";
	}
	else if (checkCode == 1)
	{
		return "审核通过";
	}
	else
	{
		return "审核未通过";
	}
}
